package com.test.demos.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.test.demos.pojo.dto.LoginDTO;
import com.test.demos.pojo.dto.RegisterDTO;
import com.test.demos.pojo.vo.UserLoginVO;
import com.test.demos.service.UsersService;
import com.test.demos.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping()
public class WebController {
    @Autowired
    private UsersService usersService;

    /**
     * 测试
     */
    @GetMapping("/test")
    public Result test() {

        return Result.ok("测试成功");
    }
    /**
     * 是不是登录
     */
    @GetMapping("/isLogin")
    public Result isLogin() {
        if (!StpUtil.isLogin()) {
            return Result.error("未登录");
        }
        return Result.ok();
    }
    /**
     * 登录功能
     */
    @SaIgnore
    @PostMapping("/login")
    public Result login(@RequestBody  LoginDTO loginDTO) {
        UserLoginVO login = usersService.login(loginDTO.getUsername(), loginDTO.getPassword());
        if (login == null) {
            return Result.error("登录失败");
        }
        return Result.ok(login);
    }


    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result logout() {
        return usersService.logout();
    }
    /**
     * 注册功能
     */
    @SaIgnore
    @PostMapping("/register")
    public Result register(@RequestBody  RegisterDTO registerDTO) {

        return usersService.register(registerDTO);
    }
}
